package com.dean.action;

import com.dean.batch.entity.DeanTree;
import com.dean.entity.DataQueryEntity;
import com.dean.entity.DeanResponse;
import com.dean.exception.DeanRuntimeException;
import com.dean.service.DbQueryService;
import com.dean.service.DeanJobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName DbQueryAction
 * @Description entity生成控制
 * @Author Administrator
 * @Date 2021/10/2315:31
 */
public class DeanJobAction {
    private final Logger logger = LoggerFactory.getLogger(DeanJobAction.class);

    private static class DbQueryActionHolder{
        private final static DeanJobAction INSTANCE = new DeanJobAction();
    }

    public static DeanJobAction getInstance(){
        return DeanJobAction.DbQueryActionHolder.INSTANCE;
    }

    public DataQueryEntity query(){
        DataQueryEntity queryEntity = new DataQueryEntity();
        try {
            DeanJobService jobService = DeanJobService.getInstance();
            queryEntity = jobService.query();
        }catch (Exception e){
            if(e instanceof DeanRuntimeException){
                DeanRuntimeException exception = (DeanRuntimeException) e;
                queryEntity.setCode(exception.getCode());
                queryEntity.setMsg(exception.getMsg());
            }else{
                logger.error(e.getMessage(),e);
                queryEntity.setCode(999);
                queryEntity.setMsg("未知错误类型,请联系管理员");
            }
        }
        return queryEntity;
    }

    public DeanResponse<DeanTree> queryChild(String jobId){
        try {
            DeanJobService jobService = DeanJobService.getInstance();
            DeanTree root = new DeanTree();
            root.setName(jobId);
            jobService.queryChild(root);
            return DeanResponse.ok(root);
        }catch (Exception e){
            if(e instanceof DeanRuntimeException){
                DeanRuntimeException exception = (DeanRuntimeException) e;
                return DeanResponse.error(exception.getCode(),exception.getMsg());
            }else{
                logger.error(e.getMessage(),e);
                return DeanResponse.error(999,e.getMessage());
            }
        }
    }






}
